package com.frxuexi.mdm2202

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
import com.frxuexi.mdm2202.ui.MainPage
import com.frxuexi.mdm2202.ui.WelcomePage
import com.frxuexi.mdm2202.ui.theme.MDM2202Theme
import com.frxuexi.mdm2202.viewmodel.AppViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(arrayOf("android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.ACCESS_COARSE_LOCATION","android.permission.ACCESS_FINE_LOCATION","android.permission.ACCESS_WIFI_STATE"),100)
        setContent {
            MDM2202Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val vm:AppViewModel= hiltViewModel()
                    //创建 NavController
                    val navController = rememberNavController()
                    val state by vm.isFirst.collectAsState()
                    if(state){
                        MainPage(navController = navController)
                    }else{
                        WelcomePage()
                        lifecycleScope.launch {
                            val timer = async { delay(3000) }
                            timer.await()
                            vm.emitFirst(true)
                        }
                    }

                    //startDestination：起始页面
                    /*NavHost(navController = navController, startDestination = NavPath.LOGIN) {
                        //登录页面
                        composable(NavPath.LOGIN) { LoginPage(navController) }
//                        composable(NavPath.NEWS + "/{myname}") {
//                            val name = it.arguments?.getString("myname")
//                            NewsPage(name=name!!) }
                        composable(NavPath.NEWS + "/{myname}?age={age}", arguments = listOf(
                            navArgument("age"){
                                type= NavType.IntType
                                defaultValue=0
                            }
                        )) {
                            val name = it.arguments?.getString("myname")
                            val age = it.arguments?.getInt("age")
                            NewsPage(name=name!!+age) }
                    }*/

                }
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MDM2202Theme {
//        Greeting("World")
//        MyCom()
//        ConstraintLayoutContent()
    }
}